package utils.load
{
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	
	import utils.LocalCache;

	public class SLoader extends Loader  
	{
		private var _isCompleted:Boolean=false;
		private var _loaderInfo:LoaderInfo;
		private var _isEncrypt:Boolean;
		/**
		 * 是否采用load缓存。 
		 */
		private var _useCache:Boolean=true;
		private var _content:*;
		public function SLoader(useCache:Boolean=true)
		{
			_useCache=useCache
			super();
		}
		
        override public function get content():DisplayObject{
		     return _content;
		}
		
		private function loadFile(request:URLRequest, context:LoaderContext=null):void{
			var bit:ByteArray=LocalCache.getInstance().getRes(request.url);
			if(request.url=="") return;
			if(bit){
				bit.position=0;
				release(bit,context);
				return;
			}
		
		    var l:URLStream=new URLStream();
		
			l.addEventListener(Event.COMPLETE,onFileCompleted);
		    l.addEventListener(ProgressEvent.PROGRESS,onPress);
			l.addEventListener(IOErrorEvent.IO_ERROR,onError);
			l.load(request);
			function onPress(e:ProgressEvent):void{
				 dispatchEvent(e);
			}
			function onError(e:IOErrorEvent):void{
				l.removeEventListener(Event.COMPLETE,onFileCompleted);
				l.removeEventListener(ProgressEvent.PROGRESS,onPress);
				l.removeEventListener(IOErrorEvent.IO_ERROR,onError);
			}
			function onFileCompleted(e:Event):void{
				l.removeEventListener(Event.COMPLETE,onFileCompleted);
				l.removeEventListener(ProgressEvent.PROGRESS,onPress);
				l.removeEventListener(IOErrorEvent.IO_ERROR,onError);
				
				var stream:URLStream=e.currentTarget as URLStream;
				var byte1:ByteArray=new ByteArray();
				stream.readBytes(byte1,0,stream.bytesAvailable);
				LocalCache.getInstance().addRes(request.url,byte1);
				if(context==null){
					context= new LoaderContext();
					
				}
				context.allowCodeImport=true;
				release(byte1,context);
			}
		}
		
		protected function release(bytes:ByteArray,context:LoaderContext):void{
			if(bytes.length<=0) return ;
			if(bytes.readUTFBytes(7)!="FENGYOU" || bytes.bytesAvailable<=200){
				
				loadBytes(bytes,context);
				bytes.clear();
				return;
			}
			var CT:int=100;
			var byte:ByteArray=new ByteArray();
			bytes.position=7;
			bytes.readBytes(byte);
			bytes.clear();
			byte.position=0;
			var temp:ByteArray=new ByteArray();
			byte.readBytes(temp,0,CT);
			
			var temp1:ByteArray=new ByteArray()
			byte.position=byte.length-CT;
			byte.readBytes(temp1,0,CT);
			
			byte.position=0;
			byte.writeBytes(temp1,0,CT);
			byte.position=byte.length-CT;
			byte.writeBytes(temp,0,CT);
			loadBytes(byte,context);
			byte.clear();
		}
		private function onError(e:IOErrorEvent):void{
			trace("加载失败:"+e)
		    this.dispatchEvent(e);
		}
		override public function load(request:URLRequest, context:LoaderContext=null):void{	 
			 request.url=LoaderManager.urlFormat(request.url);
             if(LoaderManager.cache.containsKey(request.url) && _useCache){
				var l:SLoader=SLoader(LoaderManager.cache.get(request.url));
				if(l.isCompleted){
					_loaderInfo=l.contentLoaderInfo;
					_content=l.content;
				    dispatchEvent(new Event(Event.COMPLETE));
				}else{
					l.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleted);
					l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
					//l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onPress);
				}
			    return;
			 }
			 if(_useCache){
			 	LoaderManager.cache.put(request.url,this);
			 }
			 LoaderManager.addTask(this);
			 _isCompleted=false;
			 contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleted);
			 contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
		     loadFile(request,context);
		}

        private function onCompleted(e:Event):void{
		     _isCompleted=true;
			  try{
				  if(super.content!=null){
				 	  _content=super.content;
				  }else{
					  _content=LoaderInfo(e.currentTarget).loader.content;
				  }
				 _loaderInfo=LoaderInfo(e.currentTarget);
				  e.currentTarget.removeEventListener(Event.COMPLETE,onCompleted);
			      dispatchEvent(e);
			  }catch(e:Error){
			     trace(e);
			  }
		}
		
		override public function get contentLoaderInfo():LoaderInfo{
			if(_loaderInfo) return _loaderInfo;
			return super.contentLoaderInfo;
		}
		
		/**
		 *
		 * @param url  URL字符串。
		 * @param context
		 *
		 */
		public function loadByUrl(url:String, context:LoaderContext=null,isEncrypt:Boolean=false):void{
			var u:URLRequest=new URLRequest(LoaderManager.urlFormat(url));
			if(LoaderManager.cache.containsKey(url)){
				var l:SLoader=SLoader(LoaderManager.cache.get(url))
				if(l.isCompleted){
					_loaderInfo=l.contentLoaderInfo;
					_content=l.content;
					dispatchEvent(new Event(Event.COMPLETE));
				}else{
					l.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleted);
					l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
				}
				return;
			}
			
			super.load(u,context);
			LoaderManager.cache.put(u.url,this);
			LoaderManager.addTask(this);
			_isCompleted=false;
			contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleted);
			contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
			if(_isEncrypt){
				loadFile(u,context);
			}else{
				super.load(u,context);
			}
		}
		
		public function get isCompleted():Boolean
		{
			return _isCompleted;
		}
	}
}